import axios from 'axios'
import { BaseRes, post } from '../libs/request'
import { IArticle, IComment, IFollow, ILike, IUser } from '../types'

interface If extends BaseRes {
  result: boolean
}

interface res extends BaseRes {
  result: string
}

interface userData extends BaseRes {
  data: IUser
}

interface likesData extends BaseRes {
  rows: ILike[]
}

interface commentsData extends BaseRes {
  rows: IComment[]
}

interface followsData extends BaseRes {
  rows: IFollow[]
}

interface articlesData extends BaseRes {
  rows: IArticle[]
}
// 注册
export async function register(username: string, password: string) {
  const res = await post<res>('/api/user/register', { username, password })
  return res.data
}

// 登录
export async function login(username: string, password: string) {
  const res = await post<res>('/api/user/login', { username, password })
  return res.data
}

// 当前用户信息
export async function userInfo(token?: string) {
  const res = await post<userData>('/api/user/userInfo', {}, token)
  return res.data
}

// id获取用户信息
export async function getInfo(id: string) {
  const res = await post<userData>('/api/user/getInfo', { id })
  return res.data
}

// 编辑个人信息
export async function edit(
  nickname: string,
  introduction: string,
  token?: string
) {
  const res = await post<userData>(
    '/api/user/edit',
    {
      nickname,
      introduction
    },
    token
  )
  return res.data
}

// 修改密码
export async function editPwd(
  prePassword: string,
  newPassword: string,
  token?: string
) {
  const res = await post<userData>(
    '/api/user/editPwd',
    {
      prePassword,
      newPassword
    },
    token
  )
  return res.data
}

// 查看是否关注
export async function findFollow(userId: string, token?: string) {
  const res = await post<If>('/api/user/follow/find', { userId }, token)
  return res.data
}

// 关注、取关
export async function follow(state: boolean, userId: string) {
  const res = await post<BaseRes>('/api/user/follow', { state, userId })
  return res.data
}

// 获取用户点赞
export async function likes(id: string) {
  const res = await post<likesData>('/api/user/likes', { id })
  return res.data
}

// 获取用户评论
export async function comments(id: string) {
  const res = await post<commentsData>('/api/user/comments', { id })
  return res.data
}

// 获取用户关注
export async function follows(id: string) {
  const res = await post<followsData>('/api/user/follows', { id })
  return res.data
}

// 获取用户文章
export async function articles(id?: string) {
  const res = await post<articlesData>('/api/user/articles', { id })
  return res.data
}

// 头像
export async function userAvatar(file: File) {
  const data = new FormData()
  data.append('file', file)
  const result = await axios.post('/api/user/upload', data)
  return result.data
}
